# _*_ coding : utf-8 _*_
import win32gui
import win32api
import win32con
from win32con import *
import win32clipboard as w
import time
import datetime
import random as rand
import threading
lock = threading.Lock()

lis = ['#签到', '#打卡','#美滋滋']
lis_words = ["鱼子酱", "大酱汤", "炒乌冬", "乌冬面", "醋饭", "鲷鱼烧", "拉面", "海鲜汤", "牛尾汤", "橙醋蟹",
        "海胆汤", "炒牡蛎", "大阪烧", "春卷", "泡菜烧", "鸡肉串", "铜锣烧", "荞麦面", "鳗鱼饭", "苹果饼",
        "蒙多排骨", "奥恩汤", "薇恩豆腐", "炸发条", "安尼鱼", "佐伊饺子", "芮尔饼", "瑞兹煮", "扎克烧", "卡兹克锅", "璐璐烧麦", "五郎红肠", "叔又饿了",
        "奶酪饼", "阿富汗披萨", "玉子烧,", "糖醋排骨", "明太鱼", "鳕鱼烧", "五花肉", "章鱼烧", "西京烧", "麻婆豆腐",
        "担担面", "里脊", "牛霖肉", "三文鱼,", "酒蒸蛤蜊", "关东煮", "寿喜锅", "三明治", "蛋包饭", "牛肉饭", "石狩锅",
        "昆布", "海苔", "酱汁", "水饺", "饺子", "煎饺", "蒸饺", "酱油拌面", "牛油果", "牛腩", "牛肚", "牛肝", "鸡蛋烧",
        "鱿鱼汤", "鲤鱼汤", "乌贼面", "寿司,", "冷面", "炒年糕", "安康鱼", "纳豆饭", "油泼面", "山葵盖饭", "蚬汤", "蚬子面",
        "生姜烧", "鳜鱼", "蒜蓉汤", "鲑鱼", "杏鲍菇", "鲍鱼", "味噌汤", "烤肉", "炸鸡块", "羊排", "番茄炒蛋", "肝盖饭",
        "信玄袋", "鲔鱼汤", "喜知次", "龙虾", "肉串", "鸡蛋卷", "大福", "牛筋", "梅子酒", "线鱼脊", "云吞面", "刺鲳鱼",
        "虾肉山芋", "汉堡肉", "番茄意面", "排骨面", "咖喱饭", "咖喱牛肉", "蕃茄豆腐", "秋刀鱼", "什锦面", "鱼生碎",
        "葱炒肉", "熏肠", "沙丁鱼", "什锦拌,", "花蛤", "糯米饭", "鸡肉春卷", "炒培根", "红糟排骨", "黑炒饭", "麻辣汤",
        "竹荚鱼", "玉米饼", "腊肉", "爆羊肉", "绿麻豆腐", "烧虾仁", "鱼丸面", "煲仔饭", "肠炒面", "青花鱼", "横膈膜肉",
        "麻婆凉面", "肉汁乌冬", "奶油海鲜", "薄切牛肉", "炸火腿", "炸酱面", "乌鱼子", "银鲑", "咖喱炒面", "葱花蛋",
        "仁当牛", "鸡酱烧", "蕈菇海螺", "炖牛舌", "蟹面", "炒酱面", "烘肉卷", "鸡肉锅", "马头鱼", "方头鱼", "菌菇汤",
        "寿喜烧", "小圆", "甜点", "牛舌", "泡菜鱼", "牛肉煮", "盐渍鲭鱼意面", "虾仁", "海鲜饭", "牛肉饭", "鲣鱼"]
lis_words_count = len(lis_words)

def time_count(interval=20):
    # t1 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    t1 = datetime.datetime.now().strftime("%H:%M:%S")
    print('current time is [{}]'.format(t1))
    # t2 = (datetime.datetime.now()+datetime.timedelta(minutes=5)).strftime("%Y-%m-%d %H:%M:%S")
    t2 = (datetime.datetime.now() +
          datetime.timedelta(minutes=interval)).strftime("%H:%M:%S")
    print('\n[{}] woulde be incoming.'.format(t2))


def getContentFromClipboared_u(content="寿喜锅"):

    # 打开剪贴板
    w.OpenClipboard()
    # 清空剪贴板
    w.EmptyClipboard()
    # 设置剪贴板内容
    # w.SetClipboardData(win32con.CF_UNICODETEXT, "寿喜锅")
    w.SetClipboardData(win32con.CF_UNICODETEXT, content)
    # 获取剪贴板内容
    date = w.GetClipboardData()
    # 关闭剪贴板
    w.CloseClipboard()


def mykey_press_release_u(keyno:int = 1):
    # key 1 : 0x31 or 49
    # key 2 : 0x32 or 50
    # key 3 : 0x33 or 51
    # ctrl  : 0x11 or 17
    # alt   : 0x12 or 18

    # 按下ctrl+v
    # win32api.keybd_event(0x11, 0, 0, 0)
    # win32api.keybd_event(0x56, 0, 0, 0)
    # win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
    # win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    # time.sleep(1)
    # # 按下回车
    # win32api.keybd_event(0x0D, 0, 0, 0)
    # win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
    if keyno == 1:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x31, 0, 0, 0)
        win32api.keybd_event(0x31, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 2:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x32, 0, 0, 0)
        win32api.keybd_event(0x32, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 3:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x33, 0, 0, 0)
        win32api.keybd_event(0x33, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 4:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x34, 0, 0, 0)
        win32api.keybd_event(0x34, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 5:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x35, 0, 0, 0)
        win32api.keybd_event(0x35, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 6:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x36, 0, 0, 0)
        win32api.keybd_event(0x36, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)

def findwindowfrommouse_daka30(keyno:int=1, interval:int=30):
    # time.sleep(2)
    mykey_press_release_u(keyno)
    phw = win32gui.WindowFromPoint(win32api.GetCursorPos())
    time.sleep(2)
    time_count(interval)
    # print('phw', phw)
    # print('hex(phw)', hex(phw))
    ##title = win32gui.GetWindowText(phw)
    # print(title)
    ## myposition = win32gui.GetWindowRect(phw)
    # print(myposition)

    # getContentFromClipboared()
    # # 打开剪贴板
    # w.OpenClipboard()
    # # 清空剪贴板
    # w.EmptyClipboard()
    # # 设置剪贴板内容
    # w.SetClipboardData(win32con.CF_UNICODETEXT, "寿喜锅")
    # # 获取剪贴板内容
    # date = w.GetClipboardData()
    # # 关闭剪贴板
    # w.CloseClipboard()

    # 显示窗口 (1347, 761) 1394, 764
    win32gui.ShowWindow(phw, win32con.SW_SHOW)
    win32api.SetCursorPos([1347, 761])
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 1394, 764, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 1394, 764, 0, 0)
    time.sleep(2)

    # getContentFromClipboared_u(random.choice(lis1) + ',')
    if keyno != 1:
        getContentFromClipboared_u(lis[1])
    else:
        getContentFromClipboared_u(lis[2])

    # 把剪切板内容粘贴到窗口
    win32gui.SendMessage(phw, win32con.WM_PASTE, 0, 0)
    time.sleep(0.5)
    # 按下后松开回车键，发送消息
    win32gui.SendMessage(phw, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(phw, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
    time.sleep(1)  # 延缓进程
    if keyno == 3:
        old_idx = -1
        for i in range(4):
            tmp_idx = rand.randint(0, lis_words_count-1)
            if tmp_idx != old_idx:
                old_idx = tmp_idx
            else:
                tmp_idx = lis_words_count % 7
            getContentFromClipboared_u(lis_words[tmp_idx])
            win32gui.SendMessage(phw, win32con.WM_PASTE, 0, 0)
            time.sleep(0.5)
            win32gui.SendMessage(phw, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
            win32gui.SendMessage(phw, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
            time.sleep(3)
        

def findwindowfrommouse_daka30_old(keyno:int=1, interval:int=30):
    # time.sleep(2)
    mykey_press_release_u(keyno)
    time.sleep(2)
    time_count(interval)
    phw = win32gui.WindowFromPoint(win32api.GetCursorPos())
    # print('phw', phw)
    # print('hex(phw)', hex(phw))
    title = win32gui.GetWindowText(phw)
    # print(title)
    myposition = win32gui.GetWindowRect(phw)
    # print(myposition)

    # getContentFromClipboared()
    # # 打开剪贴板
    # w.OpenClipboard()
    # # 清空剪贴板
    # w.EmptyClipboard()
    # # 设置剪贴板内容
    # w.SetClipboardData(win32con.CF_UNICODETEXT, "寿喜锅")
    # # 获取剪贴板内容
    # date = w.GetClipboardData()
    # # 关闭剪贴板
    # w.CloseClipboard()

    # 显示窗口 (1347, 761) 1394, 764
    win32gui.ShowWindow(phw, win32con.SW_SHOW)
    win32api.SetCursorPos([1347, 761])
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 1394, 764, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 1394, 764, 0, 0)
    time.sleep(2)

    # getContentFromClipboared_u(random.choice(lis1) + ',')
    if keyno != 2:
        getContentFromClipboared_u(lis[1])
    else:
        getContentFromClipboared_u(lis[2])

    # 把剪切板内容粘贴到窗口
    win32gui.SendMessage(phw, win32con.WM_PASTE, 0, 0)
    time.sleep(1)
    # 按下后松开回车键，发送消息
    win32gui.SendMessage(phw, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(phw, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
    time.sleep(1)  # 延缓进程

def run_daka(interval: int = 30):
    for i in range(42):
        findwindowfrommouse_daka30(3, interval)
        findwindowfrommouse_daka30(4, interval)
        findwindowfrommouse_daka30(5, interval)
        findwindowfrommouse_daka30(6, interval)
        time.sleep(60 * interval)

def consumer_noproduct(iinterval:int = 30, keynum:int = 2):
    for i in range(41):
        time.sleep(1.5)
        # print("---wait for producer running---")
        print("cser-current thread name is %s" %
              threading.current_thread().name)
        # print("wait-6-seconds---consumer finish---No.:[%s]" % item)
        lock.acquire()
        # fun_daka(1, "" , iinterval, keynum, word)
        findwindowfrommouse_daka30(keynum, iinterval)
        # time.sleep(2)
        lock.release()
        time.sleep(60 * iinterval + 2)
        

def run_daka_lock(interval: int = 30):
    dict1 = {"iinterval":35, "keynum": 1}
    dict2 = {"iinterval":30, "keynum": 2}
    dict3 = {"iinterval":20, "keynum": 3}
    # dict4 = {"num": 40, "iinterval":30, "keynum": 4}
    # dict5 = {"num": 40, "iinterval":30, "keynum": 5}
    # dict6 = {"num": 40, "iinterval":30, "keynum": 6}

    t1 = threading.Thread(target=consumer_noproduct, kwargs=dict1)
    t2 = threading.Thread(target=consumer_noproduct, kwargs=dict2)
    t3 = threading.Thread(target=consumer_noproduct, kwargs=dict3)
    # t4 = threading.Thread(target=consumer_noproduct, kwargs=dict4)
    # t5 = threading.Thread(target=consumer_noproduct, kwargs=dict5)
    # t6 = threading.Thread(target=consumer_noproduct, kwargs=dict6)
    
    t1.start()
    time.sleep(6)
    t2.start()
    time.sleep(6)
    t3.start()
    # t4.start()
    # t5.start()
    # t6.start()
    
    t1.join()
    t2.join()
    t3.join()
    # t4.join()
    # t5.join()
    # t6.join()
    
if __name__ == '__main__':
    # findwindowfrommouse_daka30()
    # run_daka(30)
    run_daka_lock(30)
    print('Hello Python!')

